﻿using MediatR;
using MonentExam1013.Api.Read.Appliaction.Commond;
using MonentExam1013.Api.Read.Dto;
using MonentExam1013.Domain;
using MonentExam1013.ErrorCode;
using MonentExam1013.infrastrunceture;

namespace MonentExam1013.Api.Read.Appliaction.CommondHandler
{
    public class ShowRoleCommondHandler : IRequestHandler<ShowRoleCommond, ApiResult<ApiPaging<ShowRoleDto>>>
    {
        private readonly IBaseIrepository<Role> role;

        public ShowRoleCommondHandler(IBaseIrepository<Role> role)
        {
            this.role = role;
        }
        public Task<ApiResult<ApiPaging<ShowRoleDto>>> Handle(ShowRoleCommond request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<ShowRoleDto>> result = new ApiResult<ApiPaging<ShowRoleDto>>();
            result.Code = ApiEnum.查询成功;
            result.Msg = ApiEnum.查询成功.ToString();
            var list = from a in role.GetAll()
                       select new ShowRoleDto
                       {
                           RoleId=a.RoleId,
                           RoleName=a.RoleName,
                           Name=a.Name,
                           Number=a.Number,
                           Createtime=a.Createtime,
                           States=a.States,
                           Desc=a.Desc,
                       };
            var totoCount = list.Count();
            var totoPage = (int)Math.Ceiling(totoCount * 1.0 / request.Pagesize);
            list = list.OrderBy(x => x.RoleId).Skip((request.Pageindex - 1) * request.Pagesize).Take(request.Pagesize);
            ApiPaging<ShowRoleDto> paging = new ApiPaging<ShowRoleDto>();
            paging.totoPage = totoPage;
            paging.totoCount = totoCount;
            paging.data = list.ToList();
            result.data = paging;
            return Task.FromResult(result);
        }
    }
}
